﻿using System;
using Eve.TapToClick.Models;
using Eve.TapToClick.Core.Control;
using Eve.TapToClick.Core.Device;

/// <summary>
/// 核心控制
/// 通过此类计算触碰版移动距离方向，然后发送至Windmows User32 API
/// 最多5指操作,多余不支持。
/// </summary>
namespace Eve.TapToClick.Core
{
    class CoreRestraint
    {
        // 配置文件
        private AppConfiguration appConfiguration;
        // 指针设备管理
        PointingDeviceManage pointingDeviceManage;
        // 鼠标按钮
        private MouseButtonCase mouseButtonCase;
        // 1个触碰点
        private Control1 c1;
        // 2个触碰点
        private Control2 c2;
        // 3个触碰点
        private Control3 c3;
        // 4个触碰点
        private Control4 c4;
        // 5个触碰点
        private Control5 c5;
        // 启用核心控制
        public bool Enable;

        public CoreRestraint(AppConfiguration appConfiguration, PointingDeviceManage pointingDeviceManage)
        {
            Enable = true;
            this.appConfiguration = appConfiguration;
            this.pointingDeviceManage = pointingDeviceManage;
            this.mouseButtonCase = new MouseButtonCase();
            c1 = new Control1(this.appConfiguration, this.mouseButtonCase);
            c2 = new Control2(this.appConfiguration, this.mouseButtonCase);
            c3 = new Control3(this.appConfiguration, this.mouseButtonCase);
            c4 = new Control4(this.appConfiguration, this.mouseButtonCase);
            c5 = new Control5(this.appConfiguration, this.mouseButtonCase);
        }

        /// <summary>
        /// 执行操作(在首次触碰时调用)
        /// </summary>
        /// <param name="tapData">触碰版事实返回数据</param>
        public void PerformActionBeg(TapData tapData)
        {
            Core(tapData, 0);
        }

        /// <summary>
        /// 执行操作(在第一次之后最后一次之前调用)
        /// </summary>
        /// <param name="tapData">触碰版事实返回数据</param>
        public void PerformActionWait(TapData tapData)
        {
            Core(tapData, 1);
        }

        /// <summary>
        /// 执行操作(在最后一次调用)
        /// </summary>
        /// <param name="tapData">触碰版事实返回数据</param>
        public void PerformActionEnd(TapData tapData)
        {
            Core(tapData, 2);
        }

        /// <summary>
        /// 执行操作核心方法(整合)
        /// </summary>
        /// <param name="tapData">触碰版事实返回数据</param>
        /// <param name="type">0.beg 1.wait 2.end</param>
        private void Core(TapData tapData, int type)
        {
            // 是否启用触碰板
            if (CheckEnable())
            {
                // 重置某些控制器
                for (int i = tapData.MaximumActiveContacts + 1; i < 6; i++)
            {
                GetControl(i).Clean();
            }
                // 操作某些控制器
                MainControl control = GetControl(tapData.MaximumActiveContacts);
                int j = tapData.MaximumActiveContacts;
                if (control != null)
                {
                    if (type == 0)
                    {
                        control.setFirst(tapData.InstantaneousActiveContacts[0], tapData.Start);
                        control.setPressureBeg(tapData.Pressure[0]);
                        control.addPos(tapData.PreviousXValues[0], tapData.PreviousYValues[0], tapData.TotalContactDistances[0]);
                        control.First();
                    }
                    else if (type == 1)
                    {
                        control.setPressureWait(tapData.Pressure[0]);
                        control.addPos(tapData.PreviousXValues[0], tapData.PreviousYValues[0], tapData.TotalContactDistances[0]);
                        control.Sustained();
                    }
                    else if (type == 2)
                    {
                        control.setLast(tapData.InstantaneousActiveContacts[0], tapData.Start);
                        control.Last();
                    }
                }
            }
        }

        private MainControl GetControl(int index)
        {
            switch (index)
            {
                case 1:
                    return c1;
                case 2:
                    return c2;
                case 3:
                    return c3;
                case 4:
                    return c4;
                case 5:
                    return c5;
            }
            return null;
        }

        private bool CheckEnable()
        {
            if (Enable && appConfiguration.Enable)
            {
                if(pointingDeviceManage.GetDeviceNum() <= appConfiguration.PointingDeviceMaxEnableNum)
                {
                    return true;
                }
            }
            return  false;
        }

    }
}
